package com.party.admin.biz.openPlatform;

import com.party.common.constant.Constant;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.*;
import com.party.core.model.wechat.WechatAccount;
import com.party.core.model.wechat.WechatMerchants;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberExternalAppService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberMerchantService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.wechat.IWechatAccountService;
import com.party.core.service.wechat.IWechatMerchantsService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 微信开放平台配置
 * Created by wei.li.
 *
 * @date 2018/1/22 0022
 * @time 17:06
 */
@Service
public class WechatOpenMemberBizService {

    @Autowired
    private IMemberMerchantService memberMerchantService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMemberExternalAppService memberExternalAppService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IWechatMerchantsService wechatMerchantsService;

    @Autowired
    private IWechatAccountService wechatAccountService;

    private String name = "同行者CEO";

    @Value("#{wechat['wechat.wwz.headimgurl']}")
    private String headimgurl;

    @Value("#{wechat['wechat.wwz.qrurl']}")
    private String qrurl;


    private String certUrl = "http://tongxingzhe-10052192.cos.myqcloud.com/cert/1305970801/apiclient_cert.p12";

    public void init(){

        WechatOpenMember wechatOpenMemberSystem = wechatOpenMemberService.getSystem();
        WechatMerchants wechatMerchantsSystem = wechatMerchantsService.getSystem();

        // 循环member_merchant
        List<MemberMerchant> list = memberMerchantService.list(new MemberMerchant());
        boolean isOK = false;
        for (MemberMerchant m : list){

            //初始化商户开关
            MemberGroup member = memberGroupService.get(m.getMemberId());
            member.setOpenStatus(m.getOpenStatus());
            member.setMandatoryAttention(m.getMandatoryAttention());
            member.setDeployType(m.getDeployType());
            member.setOpenStatus(1);
            memberGroupService.update(member);

            //初始化 wechat_open_member
            WechatAccount wechatAccount = wechatAccountService.findByMemberId(m.getMemberId());
            WechatOpenMember wechatOpenMember = new WechatOpenMember();
            wechatOpenMember.setAuthorizerAppid(m.getOfficialAccountId());
            wechatOpenMember.setNickName(m.getOfficialAccountName());
            wechatOpenMember.setSecret(m.getOfficialAccountSecret());
            wechatOpenMember.setQrcodeUrl(m.getQrUrl());
            wechatOpenMember.setHeadImg(m.getHeadImg());
            wechatOpenMember.setDeployType(DeployType.FILL_IN.getCode());
            if (null != wechatAccount){
                wechatOpenMember.setAuthorizerAccessToken(wechatAccount.getToken());
                wechatOpenMember.setTicket(wechatAccount.getTicket());
            }
            if (wechatOpenMemberSystem.getAuthorizerAppid().equals(wechatOpenMember.getAuthorizerAppid())){
                wechatOpenMember.setType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM);
                isOK = true;
            }
            else {
                wechatOpenMember.setType(Constant.WECHAT_ACCOUNT_TYPE_PARTNER);
            }
            wechatOpenMemberService.insert(wechatOpenMember);


            //初始化 wechat_merchants
            WechatMerchants wechatMerchants = new WechatMerchants();
            wechatMerchants.setMerchantName(m.getMerchantName());
            wechatMerchants.setMerchantId(m.getMerchantId());
            wechatMerchants.setMerchantApiKey(m.getMerchantApiKey());
            wechatMerchants.setCertUrl(m.getCertUrl());
            if (wechatOpenMemberSystem.getAuthorizerAppid().equals(wechatOpenMember.getAuthorizerAppid())){
                wechatMerchants.setType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM);
            }
            else {
                wechatMerchants.setType(Constant.WECHAT_ACCOUNT_TYPE_PARTNER);
            }
            wechatMerchantsService.insert(wechatMerchants);

            // 初始化 member_external_app
            MemberExternalApp memberExternalApp
                    = memberExternalAppService.getSet(m.getMemberId(), DeployType.FILL_IN.getCode());
            memberExternalApp.setExternalAccountId(wechatOpenMember.getId());
            memberExternalApp.setExternalMerchantId(wechatMerchants.getId());
            memberExternalAppService.update(memberExternalApp);
        }

        //系统配置信息
        if (!isOK) {
            WechatAccount wechatAccount = wechatAccountService.getSystem();
            WechatOpenMember sys = new WechatOpenMember();
            sys.setAuthorizerAppid(wechatOpenMemberSystem.getAuthorizerAppid());
            sys.setNickName(name);
            sys.setSecret(wechatOpenMemberSystem.getSecret());
            sys.setQrcodeUrl(qrurl);
            sys.setHeadImg(headimgurl);
            sys.setDeployType(DeployType.FILL_IN.getCode());
            sys.setAuthorizerAccessToken(wechatAccount.getToken());
            sys.setTicket(wechatAccount.getTicket());
            sys.setType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM);
            wechatOpenMemberService.insert(sys);

            WechatMerchants wechatMerchants = new WechatMerchants();
            wechatMerchants.setMerchantName(name);
            wechatMerchants.setMerchantId(wechatMerchantsSystem.getMerchantId());
            wechatMerchants.setMerchantApiKey(wechatMerchantsSystem.getMerchantApiKey());
            wechatMerchants.setCertUrl(certUrl);
            wechatMerchants.setType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM);
            wechatMerchantsService.insert(wechatMerchants);
        }

        //循环授权信息
        WechatOpenMember wechatOpenMember = new WechatOpenMember();
        wechatOpenMember.setDeployType(DeployType.AUTHORIZATION.getCode());
        List<WechatOpenMember> wechatOpenMemberList = wechatOpenMemberService.list(wechatOpenMember);
        for (WechatOpenMember w : wechatOpenMemberList){

            //初始化 member_external_app
            MemberExternalApp  memberExternalApp
                    = memberExternalAppService.getSet(w.getMemberId(), DeployType.AUTHORIZATION.getCode());
            memberExternalApp.setExternalAccountId(w.getId());
            memberExternalAppService.update(memberExternalApp);
        }

    }


    public void initExternalApp(){
        List<MemberGroup> partnerList = memberGroupService.getAllPartnerNoOpenStatus();
        for (MemberGroup partner : partnerList){
            MemberExternalApp memberExternalApp
                    = memberExternalAppService.getSet(partner.getId(), DeployType.FILL_IN.getCode());
            if (null == memberExternalApp.getExternalAccountId()){
                WechatOpenMember wechatOpenMember = wechatOpenMemberService.getSystem();
                WechatMerchants wechatMerchants = wechatMerchantsService.getSystem();
                memberExternalApp.setExternalAccountId(wechatOpenMember.getId());
                memberExternalApp.setExternalMerchantId(wechatMerchants.getId());
                memberExternalAppService.update(memberExternalApp);

                //初始化商户开关
                MemberGroup m = memberGroupService.get(partner.getId());
                m.setDeployType(Constant.DEPLOY_TYPE_FILL);
                m.setOpenStatus(1);
                memberGroupService.update(m);
            }
        }
    }
}
